package com.qdu.bean;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class TimeBean implements WritableComparable<TimeBean> {
    private String timestamp;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");

    int n=1;
    @Override
    public String toString() {
        return timestamp;
    }

    public TimeBean() {
        this.timestamp="";
    }

    @Override
    public int compareTo(TimeBean o) {
        try {
            long thisValue = sdf.parse(this.timestamp).getTime();
            long thatValue = sdf.parse(o.timestamp).getTime();
            return (thisValue < thatValue ? -1 :(thisValue == thatValue ? 0 : 1));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return -2;
    }

    @Override
    public void write(DataOutput dataOutput) throws IOException {
        dataOutput.writeUTF(timestamp);
        dataOutput.writeInt(n);
    }

    @Override
    public void readFields(DataInput dataInput) throws IOException {
        timestamp = dataInput.readUTF();
        n=dataInput.readInt();
    }

    public void set2(String timestamp) {
        this.timestamp = timestamp;
    }

}
